#include <stdlib.h>
#include <stdio.h>

#include "linked_list.h"

standby_t* createNode(int seat_id, int customer_id) {
    standby_t *p = malloc(sizeof(standby_t));
    p->id = seat_id;
    p->customer_id = customer_id;
    p->next = NULL;
    return p;
}

int isDuplicate(int seat_id, int customer_id, standby_t *standby_header) {
    standby_t* p = NULL;

    for (p = standby_header; p != NULL; p = p->next)
        if (p->id == seat_id && p->customer_id == customer_id)
            return 1; // found

    return 0; // not found
}

void add2StandbyList(int seat_id, int customer_id, standby_t **standby_header, int *list_length) {
    standby_t* new_node = createNode(seat_id, customer_id);

    if (*standby_header == NULL)
        *standby_header = new_node;
    else {
        standby_t* p;
        for (p = *standby_header; p->next != NULL; p = p->next); // find the last element in the list
        p->next = new_node;
    }

    (*list_length)++;

    return;
}

void removeFromStandbyList(int seat_id, standby_t **standby_header, int *list_length) { // delete all request with that
    standby_t* prev = NULL;
    standby_t* current = *standby_header;

    while (current != NULL) {
        if (current->id == seat_id) {
            if (current == *standby_header) {
                *standby_header = (*standby_header)->next;
                free(current);
                current = *standby_header;
            }
            else {
                prev->next = current->next;
                free(current);
                current = prev->next;
            }

            (*list_length)--;
        }
        else {
            prev = current;
            current = current->next;
        }
    }

    return;
}

void assignUser2Seat(int seat_id, seat_t *seat_header, standby_t **standby_header, int *list_length) { // assign user in standby list to seat
    // 1. remove the customer from the stanby list
    standby_t* prev = NULL;
    standby_t* current = *standby_header;
    int customer_id = -999;

    while (current != NULL && (current->id != seat_id)) {
        prev = current;
        current = current->next;
    }

    if (current != NULL) { // found
        customer_id = current->customer_id;

        if (current == *standby_header)
            *standby_header = (*standby_header)->next;
        else
            prev->next = current->next;

        free(current);
        (*list_length)--;
    }
    else // not found
        return;

    // 2. assign the customer to the seat
    seat_t *p = seat_header;

    while (p != NULL && p->id != seat_id)
        p = p->next;

    if (p != NULL) {
        p->customer_id = customer_id;
        p->state = PENDING;
    }

    return;
}

void emptyStandbyList(standby_t **standby_header) {
    standby_t *p = *standby_header;

    while(p != NULL)
    {
        standby_t *temp = p;
        p = p->next;
        free(temp);
    }

    *standby_header = NULL; // prevent dangling pointer
}

void display(standby_t *standby_header) { // help debugging only
    standby_t* current = standby_header;

    while (current != NULL) {
        printf("seat: %d customer: %d\n", current->id, current->customer_id);
        fflush(stdout);
        current = current->next;
    }
}
